#ifndef INC_SERVER_H
#define INC_SERVER_H

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>

// Maximum number of pending connection requests
//
const int   kMaxListen      = 5;

// Default port to use
//
const short kDefaultPort    = 9000;

// Implements the TCP/IP server
//
class Server
{
public:
    
    // Constructor
    //
    Server();
    Server(short port);
    
    // Destructor
    //
    ~Server();
    
    // Set up listening by opening a socket.
    // Returns true if successful, otherwise false.
    //
    bool open();
    
    // Close socket
    //
    bool close();
    
    // Wait to accept a client then fork a handler.
    // Returns true if ready to wait for the next client, otherwise false.
    //
    bool wait();
    
    // Talk to client as needed
    //
    void clientHandler(int fd, sockaddr_in* addr);
    
    
private:
    
    int   m_fd;                                 // file descriptor
    short m_port;                               // port to use
};

#endif  // INC_SERVER_H
